﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.LayerHelper.MP_OBS.DAL.HelperClasses;

using MP.Core.Library;
using MP.Core.Config;

public partial class Admin_Hurman_Controls_AddEditBrand : MP.Core.Authentication.Control.AuthenBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitAuthen(EnumModules.BRANCH);
        if (!IsPostBack)
        {
            loadData();
        }
    }

    public Guid Id
    {
        get { return !string.IsNullOrEmpty(Request["Id"]) ? Util.IsGuid(Request["Id"]) ? new Guid(Request["Id"]) : Guid.Empty : Guid.Empty; }
    }

    HrBranchEntity getBrand()
    {
        HrBranchEntity branh = new HrBranchEntity();

        branh.Id = Guid.NewGuid();
        branh.BranhCode = Filter.GetMaxString(txtBranhCode.Text.Trim(), HrBranchFields.BranhCode.MaxLength);
        branh.BranhName = Filter.GetMaxString(txtBranhName.Text.Trim(), HrBranchFields.BranhName.MaxLength);
        branh.Address = Filter.GetMaxString(txtAddress.Text.Trim(), HrBranchFields.Address.MaxLength);
        branh.Phone = Filter.GetMaxString(txtPhone.Text.Trim(), HrBranchFields.Phone.MaxLength);
        branh.Description = Filter.GetMaxString(txtDescription.Text.Trim(), HrBranchFields.Description.MaxLength);
        branh.UpdatedBy = Util.CurrentUserName;
        branh.UpdatedDate = DateTime.Now;

        return branh;
    }

    void loadData()
    {
        HrBranchEntity branh = HrBranchManager.CreateInstant().GetById(Id);
        if (branh != null)
        {
            if (!IsEdit())
                RedirectLogin();

            txtAddress.Text = branh.Address;
            txtBranhCode.Text = branh.BranhCode;
            txtBranhName.Text = branh.BranhName;
            txtDescription.Text = branh.Description;
            txtPhone.Text = branh.Phone;

            btnAdd.Visible = false;
            btnEdit.Visible = true;
        }
        else
        {
            if (!IsAdd())
                RedirectLogin();
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (!IsAdd())
                RedirectLogin();

            HrBranchEntity branh = getBrand();
            branh.CreatedBy = Util.CurrentUserName;
            branh.CreatedDate = DateTime.Now;

            HrBranchManager.CreateInstant().Insert(branh);

            Response.Redirect("/Admin/Hurman/ListBranh.aspx");
        }
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (!IsEdit())
                RedirectLogin();

            HrBranchEntity branh = getBrand();
            HrBranchEntity tmp = HrBranchManager.CreateInstant().GetById(Id);
            if (tmp != null)
            {
                branh.Id = tmp.Id;
                branh.CreatedBy = tmp.CreatedBy;
                branh.CreatedDate = tmp.CreatedDate;
                branh.Priority = tmp.Priority;

                HrBranchManager.CreateInstant().Update(branh);

                Response.Redirect("/Admin/Hurman/ListBranh.aspx");
            }
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/Hurman/ListBranh.aspx");
    }

    protected void validBranh_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = true;

        HrBranchEntity branh = HrBranchManager.CreateInstant().GetById(Id);
        if (branh != null) // Edit
        {
            if (branh.BranhCode != txtBranhCode.Text.Trim() && HrBranchManager.CreateInstant().IsExistsByBranchCode(txtBranhCode.Text.Trim()))
            {
                validBranh.ErrorMessage = "Trùng mã chi nhánh";
                args.IsValid = false;
            }
        }
        else // Add
        {
            if (HrBranchManager.CreateInstant().IsExistsByBranchCode(txtBranhCode.Text.Trim()))
            {
                validBranh.ErrorMessage = "Trùng mã chi nhánh";
                args.IsValid = false;
            }
        }
    }
}
